package com.imot.scheduler;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.imot.config.WebSocket;
import com.imot.controller.StatusSituationController;
import com.imot.pojo.dto.LevelDto;
import com.imot.pojo.enums.DateEnum;
import com.imot.pojo.vo.MonitorWaveVo;
import com.imot.response.Result;
import com.imot.service.StatusSituationService;
import lombok.RequiredArgsConstructor;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * ClassName: StatusSituationScheduler
 * Description:
 * date: 2023/12/6 9:26
 *
 * @author imot-jiah.wang
 */
//@Component
@RequiredArgsConstructor
public class StatusSituationScheduler {
    private final StatusSituationController statusSituationController;
    private final StatusSituationService statusSituationService;
    private final WebSocket webSocket;

    @Scheduled(fixedRate = 1000)
    public void getAlertsCount(){
        Result result = statusSituationService.getAlertsCount();
        JSONObject json = (JSONObject) JSON.toJSON(result);
        String jsonString = json.toString();
        webSocket.sendOneMessage("staGetAlertsCount",jsonString);
    }

    @Scheduled(fixedRate = 1000)
    private void getCurrentStatus(){
        Result result = statusSituationService.getCurrentStatus();
        JSONObject json = (JSONObject) JSON.toJSON(result);
        String s = json.toString();
        webSocket.sendOneMessage("staGetCurrentStatus",s);
    }

    @Scheduled(fixedRate = 1000)
    public void getThresholdEvaluation(){
        List<String> list = new ArrayList<>();
        list.add("motorSpeed");
        list.add("elecTorque");
        list.add("supplyVoltage");
        list.add("motorCurrent");
        list.add("thrust");
        list.add("temperature");
        List<MonitorWaveVo> monitorWaveVos = new ArrayList<>();
        for (String s : list) {
            MonitorWaveVo thresholdEvaluation = statusSituationService.getThresholdEvaluation(s);
            monitorWaveVos.add(thresholdEvaluation);
        }
        Result success = Result.success(monitorWaveVos);
        JSONObject json = (JSONObject) JSON.toJSON(success);
        String s = json.toString();

        webSocket.sendOneMessage("staGetThresholdEvaluation",s);
    }

    @Scheduled(fixedRate = 1000)
    public void getExceptionLevelDistribution(){
        String year = DateEnum.YEAR.toString();
        String  month = DateEnum.MONTH.toString();
        String  quarter = DateEnum.QUARTER.toString();
        List<String> list = new ArrayList<>();

        Map<String, Integer> yearMap = statusSituationService.getExceptionLevelDistribution(year);
        Map<String, Integer> monthMap = statusSituationService.getExceptionLevelDistribution(month);
        Map<String, Integer> quarterMap = statusSituationService.getExceptionLevelDistribution(quarter);
        LevelDto levelDto = new LevelDto();
        levelDto.setMonth(monthMap);
        levelDto.setQuarter(quarterMap);
        levelDto.setYear(yearMap);
        Result success = Result.success(levelDto);
        JSONObject json = (JSONObject) JSON.toJSON(success);
        String s = json.toString();
        webSocket.sendOneMessage("staGetExceptionLevelDistribution",s);
    }

}

